package com.luoyulin.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoyulin.reggie.common.CustomExeception;
import com.luoyulin.reggie.entity.Category;
import com.luoyulin.reggie.entity.Dish;
import com.luoyulin.reggie.entity.Setmeal;
import com.luoyulin.reggie.mapper.CategoryMapper;
import com.luoyulin.reggie.service.CategoryService;
import com.luoyulin.reggie.service.DishService;
import com.luoyulin.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author luoyulin
 * @create 2022-04-24 12:51
 **/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService{
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 根据ID删除分类，之前判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);

        //查询分类是否关联菜品
        if(count1 > 0){
            //已经关联菜品，抛出异常
            throw new CustomExeception("当前分类下关联了菜品！");

        }
        //查询分类是否关联套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2 > 0){
            //关联套餐
            throw new CustomExeception("当前分类下关联了套餐！");
        }
        //删除
        super.removeById(id);
    }
}
